import { MemoryStorage } from './memory.storage';
import { Model } from 'sequelize-typescript';
import config from '../config';
export enum StorageType { 
    MEMORY,
    REDIS,
}

export class StorageFactory { 
    static create<T extends Model<T>>(type: StorageType, prefix: string) {
        if (type == StorageType.MEMORY) { 
            return new MemoryStorage<T>(prefix);
        }
        throw new Error("only support memory storage type!");
    }

    static getDefault<T extends Model<T>>(prefix: string) { 
        let type: StorageType;
        let typeStr = config.cache.storage.type;
        switch (typeStr) { 
            case 'memory':
                type = StorageType.MEMORY;
                break;
            case 'redis':
                type = StorageType.REDIS;
                break;
            default:
                throw new Error("Invalid cache.storage.type value" + typeStr);
        }
        return StorageFactory.create<T>(type, prefix);
    }
}